<template>
  <div class="app-container">
    <!-- 放置表单 -->
    <el-form ref="form" :model="userData" :rules="rules" label-width="60px">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="userData.username" style="width: 300px" />
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model="userData.mobile" disabled style="width: 300px" />
      </el-form-item>
      <el-form-item label="密码:">
        <el-input
          v-model="userData.password"
          type="password"
          disabled
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateAccount">更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入更新用户信息接口
import { saveUserDetailById } from '@/api/user'
export default {
  props: {
    // 表单数据（父组件传递的数据）
    userData: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      // 表单验证规则
      rules: {
        username: [
          { required: true, min: 2, max: 6, message: '用户名长度2~6为！', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 更新用户名
    updateAccount () {
      this.$refs.form.validate(async (isok) => {
        if (isok) {
          // 验证通过
          await saveUserDetailById(this.userData)
          this.$message.success('用户名更新成功！')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
